package com.hemaapp.yjnh.adapter;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hemaapp.yjnh.activity.BankActivity;
import com.hemaapp.yjnh.bean.Bank;
import com.hemaapp.hm_FrameWork.HemaAdapter;
import com.hemaapp.yjnh.R;

/**
 * 银行列表
 */
public class BankAdapter extends HemaAdapter implements OnClickListener {

	private BankActivity bankActivity;
	private ArrayList<Bank> banks;

	public BankAdapter(BankActivity bankActivity, ArrayList<Bank> banks) {
		super(bankActivity);
		this.bankActivity = bankActivity;
		this.banks = banks;
	}

	@Override
	public int getCount() {
		int size = banks == null ? 0 : banks.size();
		return size == 0 ? 1 : banks.size();
	}

	@Override
	public boolean isEmpty() {
		int size = banks == null ? 0 : banks.size();
		return size == 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isEmpty())
			return getEmptyView(parent);

		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_bank, null);   
			holder = new ViewHolder();
			findView(convertView, holder);
			convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
		} else {
			holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
		}

		Bank bank = banks.get(position);
		setData(position, holder, bank);

		return convertView;
	}

	private void setData(int position, ViewHolder holder, Bank bank) {
		holder.bankname.setText(bank.getName());
		holder.allitem.setTag(bank);
		holder.allitem.setOnClickListener(this);
	}

	private void findView(View view, ViewHolder holder) {
		holder.allitem = view.findViewById(R.id.allitem);
		holder.bankname = (TextView) view.findViewById(R.id.bankname);
	}

	private static class ViewHolder {
		View allitem;
		TextView bankname;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.allitem:
			Bank bank = (Bank) v.getTag();
			bankActivity.setBank(bank);
			break;
		default:
			break;
		}
	}

}
